import {delay, toast} from "../../utils/tool";

const app = getApp();
Page({
    data: {
        room_id: null,
        room_data: [],
        selectIndex: 1,
        order_id: 0,
        activeIndex: 0,
        priceList: [],
        timeList: [1, 2, 4, 6, 8],
        payMethodMenu: [ // 支付方式
            {label: '微信支付', icon: '/images/wx.png', value: 1},
            {label: '余额支付', icon: '/images/yue.png', value: 2},
        ],
        payType: 1,
        userMoney: '0.00'
    },
    changPayType(ev) {
        const {value} = ev.currentTarget.dataset
        if (value === this.data.payType) return
        this.setData({
            payType: value
        })
    },
    onLoad(options) {
        this.setData({
            room_id: options.room_id,
            order_id: options.order_id
        })
    },
    initPrice(price) {
        const {timeList} = this.data
        const result = timeList.map(item => {
            const value = (price * item).toFixed(2)
            return {
                price: value,
                hour: item,
            }
        })
        this.setData({
            priceList: result
        })
    },
    selectTime(e) {
        this.setData({
            activeIndex: e.currentTarget.dataset.index
        })
    },
    initRoomDetail() {
        app.ajax({
            url: '/api/user/getRoomInfo',
            data: {
                roomId: this.data.room_id
            },
            success: res => {
                this.initPrice(res.data.price * 1)
                this.setData({
                    room_data: res.data,
                })
            }
        });
    },
    toUserAccount() {
        wx.navigateTo({
            url: '/pages/wallet/wallet?event=goBack',
        })
    },
    async initUserMoney() {
        const userInfo = await app.updateUserInfo()
        const {money} = userInfo
        this.setData({userMoney: money})
    },
    onShow() {
        this.initRoomDetail()
        this.initUserMoney().catch(null)
    },
    wxPay(data) {
        if (typeof data !== 'object') return
        if (!data.hasOwnProperty('paySign')) return;
        wx.requestPayment({
            nonceStr: data.nonceStr,
            package: data.package,
            paySign: data.paySign,
            timeStamp: data.timeStamp,
            signType: data.signType,
            fail: () => {
                toast({title: '续单失败!', icon: 'error'})
            },
            success: () => {
                toast({title: '续单成功', icon: 'success'}, () => {
                    wx.switchTab({
                        url: '/pages/index/index'
                    })
                })
            }
        })
    },
    pay() {
        const hasMobile = app.checkHasMobile()
        if (!hasMobile) return
        const {activeIndex, priceList, payType, room_id, order_id} = this.data
        const target = priceList[activeIndex]
        if (!target) return;
        wx.showLoading({title: '正在续单', mask: true})
        app.ajax({
            url: '/api/user/renewalOrder',
            data: {
                orderId: order_id,
                roomId: room_id,
                time: target.hour,
                pay_type: payType,
            },
            success: async res => {
                await delay(300)
                wx.hideLoading()
                const {code, data, msg} = res
                if (code > 0) {
                    if (payType < 2) {
                        return this.wxPay(data)
                    }
                    this.initUserMoney().catch(null)
                    return toast({title: msg, icon: 'success'}, () => {
                        wx.switchTab({
                            url: '/pages/index/index'
                        })
                    })
                }
                toast({title: msg, icon: 'error'})
            }
        });
    },
})
